﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLPMT_BUS;
using QLPMT_DTO;

namespace my_project_qlpmt1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void phânToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {

                Form_KhamBenh();
                Form_BenhNhan();
                Form_TiepNhanLoaiBenh();
                Form_CachDung();
                Form_Thuoc();
                Form_DonVi();
                Form_DonGiaThuoc();
                Form_PhieuKhamBenh();
                Form_BaoCaoDoanhThu();
                Form_ThayDoiQuiDinh();
                Form_BaoCaoSuDungThuoc();
            }
            catch
            {
            }
        }
        #region thong tin tab kham benh
        //load tab_khambenh
        public void Form_KhamBenh()
        {
            dti_ngaykham_kb.Text = DateTime.Today.Date.ToShortDateString();
            DanhSachBenhNhanCungNgayKham(dti_ngaykham_kb.Value);
            LoadDanhSachBenhNhan();

        }
        public void LoadDanhSachBenhNhan()
        {
            List<BenhNhan> list = new List<BenhNhan>();
            list = new BenhNhanBUS().GetAll();
            cbb_mabenhnhan_kb.DataSource = list;
            cbb_mabenhnhan_kb.DisplayMember = "Mabenhnhan";
            cbb_mabenhnhan_kb.ValueMember = "Hoten";
            txt_hoten_kb.Text = cbb_mabenhnhan_kb.SelectedValue.ToString();
        }
        private void cbb_mabenhnhan_kb_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_hoten_kb.Text = cbb_mabenhnhan_kb.SelectedValue.ToString();
        }
        private void dti_ngaykham_kb_TextChanged(object sender, EventArgs e)
        {
            DanhSachBenhNhanCungNgayKham(dti_ngaykham_kb.Value);

        }
        public void DanhSachBenhNhanCungNgayKham(DateTime ngay)
        {
            List<BenhNhan> list = new List<BenhNhan>();
            list = new BenhNhanBUS().DanhSachBenhNhanKhamCungNgay(ngay);
            DGV_Danhsachkhambenh_kb.DataSource = list;
            danhsothutuchodatagridview(DGV_Danhsachkhambenh_kb, "STT_KB");
            DGV_Danhsachkhambenh_kb.Columns["Gioitinh"].Visible = false;
            DGV_Danhsachkhambenh_kb.AllowUserToAddRows = true;

        }
        public void danhsothutuchodatagridview(DataGridView table, string colunmName)
        {
            int i = 0;
            while (i < table.RowCount)
            {
                table.Rows[i].Cells[colunmName].Value = i + 1;
                i++;
            }
        }
        private void btn_Them_kb_Click(object sender, EventArgs e)
        {
            if (new KhamBenhBUS().IsFull(dti_ngaykham_kb.Value) == true)
            {
                string ma = new KhamBenhBUS().GetMaKhamBenh();
                string mabenhnhan = cbb_mabenhnhan_kb.Text;
                DateTime ngay = dti_ngaykham_kb.Value;
                if (new KhamBenhBUS().ThemBenhNhanVaoDanhSachKhamBenh(ma, mabenhnhan, ngay) == true)
                {
                    MessageBox.Show("Thêm thành công!!!!!!!");
                    DanhSachBenhNhanCungNgayKham(dti_ngaykham_kb.Value);

                }
                else
                    MessageBox.Show("Chưa thêm được.Nhập lại thông tin");

            }
            else
                MessageBox.Show("Bệnh đăng kí khám đã đủ");

        }
        private void cbb_mabenhnhan_kb_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            txt_hoten_kb.Text = cbb_mabenhnhan_kb.SelectedValue.ToString();
        }
        #endregion
        #region thong tin tab benh nhan
        //load ngay thang nam cho thong tin benh nhan cho form benh nhan
        public void LoadDanhSachBenhNhanVuaTiepNhan()
        {
            List<BenhNhan> list = new List<BenhNhan>();
            list = new BenhNhanBUS().MoiTiepNhan(DateTime.Now);
            DGV_DanhSachBenhNhanMoiTrongNgay.DataSource = list;
            danhsothutuchodatagridview(DGV_DanhSachBenhNhanMoiTrongNgay, "STT_bn");
            DGV_DanhSachBenhNhanMoiTrongNgay.Columns["Gioitinh"].Visible = false;
            DGV_DanhSachBenhNhanMoiTrongNgay.Columns["NgayTiepNhan"].Visible = false;
            //DGV_Danhsachkhambenh_kb.AllowUserToAddRows = true;
        }
        public void LoadNamSinh()
        {
            List<int> ListNamSinh = new List<int>();
            for (int i = DateTime.Now.Year - 80; i < DateTime.Now.Year; i++)
            {
                ListNamSinh.Add(i);
            }
            cbb_NamSinh_bn.DataSource = ListNamSinh;
        }
        //load gioi tinh cho thong tin benh nhan form benh nhan
        public void LoadGioiTinh()
        {
            List<string> listgioitinh = new List<string>();
            listgioitinh.Add("Nam");
            listgioitinh.Add("Nữ");
            cbb_gioitinh_bn.DataSource = listgioitinh;
        }
        // load tab_benhnhan
        public string _mabenhnhan;
        public void Form_BenhNhan()
        {
            LoadNamSinh();
            LoadGioiTinh();
            LoadDanhSachBenhNhanVuaTiepNhan();
            string mabenhnhan = new BenhNhanBUS().MaBenhNhanMoi();
            //txt_MaBenhNhan_bn.Text = mabenhnhan;
            _mabenhnhan = mabenhnhan;
        }
        private void btn_them_bn_Click(object sender, EventArgs e)
        {
            string hoten = txt_HoTen_bn.Text;
            string diachi = txt_DiaChi_bn.Text;
            int NamSinh = int.Parse(cbb_NamSinh_bn.SelectedValue.ToString());
            string gioitinh = cbb_gioitinh_bn.SelectedValue.ToString();
            string mabenhnhan = _mabenhnhan;
            if (new BenhNhanBUS().ThemBenhNhanMoi(mabenhnhan, hoten, diachi, gioitinh, NamSinh, DateTime.Now) == true)
            {
                MessageBox.Show("Thêm Thành Công!!");
                txt_HoTen_bn.Text = "";
                txt_DiaChi_bn.Text = "";
                Form_BenhNhan();
                LoadDanhSachBenhNhan();
            }
            else
            {
                MessageBox.Show("Nhận không chính xác!!");
            }
            LoadDanhSachBenhNhanVuaTiepNhan();
        }
        private void dti_ngaykham_kb_ValueChanged(object sender, EventArgs e)
        {
            DanhSachBenhNhanCungNgayKham(dti_ngaykham_kb.Value);
        }
        #endregion

        private void btn_search_bn_Click(object sender, EventArgs e)
        {
            FR_TraCuuBenhNhan fr = new FR_TraCuuBenhNhan();
            fr.ShowDialog();
        }
        #region Thong tin loai benh
        //private string ma_loai_Benh;
        public void Form_TiepNhanLoaiBenh()
        {
            List<LoaiBenh> l = new LoaiBenhBUS().GetAll();
            DGV_DanhSachLoaiBenh.DataSource = l;
            //danhsothutuchodatagridview(DGV_DanhSachLoaiBenh, "STT_benh");
            //DGV_DanhSachLoaiBenh.Columns["Maloaibenh"].Visible = false;

        }
        // Them loai benh vao danh sach

        private void btn_them_loaibenh_Click(object sender, EventArgs e)
        {
            try
            {
                string ten = txt_tenloaibenh_lb.Text;
                if (new LoaiBenhBUS().InsertLoaiBenh(ten) == false)
                {
                    MessageBox.Show("Số lượng bệnh vượt quá qui định");
                    txt_tenloaibenh_lb.Text = "";
                }
                else
                {
                    MessageBox.Show("Tiếp nhận thành công");
                    Form_TiepNhanLoaiBenh();
                    txt_tenloaibenh_lb.Text = "";
                    //txt_maloaibenh_lb.Text = new LoaiBenhBUS().GetMaLoaiBenh();
                }
            }
            catch
            {
                MessageBox.Show("Số lượng bệnh vượt quá qui định");
                txt_tenloaibenh_lb.Text = "";
                return;
            }
        }
        private void DGV_DanhSachLoaiBenh_MouseClick(object sender, MouseEventArgs e)
        {
            txt_tenloaibenh_lb.Text = DGV_DanhSachLoaiBenh.SelectedRows[0].Cells[1].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string ma = DGV_DanhSachLoaiBenh.SelectedRows[0].Cells[0].Value.ToString();
                string ten = txt_tenloaibenh_lb.Text;
                LoaiBenhBUS bus = new LoaiBenhBUS();
                Boolean kq = bus.sualoaibenh(ten, ma);
                if (kq == true)
                {

                    MessageBox.Show("sửa thành công");
                    Form_TiepNhanLoaiBenh();
                }
            }
            catch
            {
                MessageBox.Show("sửa thất bại");
            }

        }
        #endregion

        private void DGV_DanhSachLoaiBenh_SelectionChanged(object sender, EventArgs e)
        {
            
        }

        

        
        #region Thong tin cach dung
        public void Form_CachDung()
        {
            List<CachDung> l = new CachDungBUS().GetAll();
            DGV_DanhSachCachDung.DataSource = l;
        }

        private void btn_them_cd_Click(object sender, EventArgs e)
        {
            try
            {
                string ten = txt_tencachdung_cd.Text;
                if (new CachDungBUS().InsertCachDung(ten) == true)
                {
                    MessageBox.Show("Tiếp nhận thành công", "Thông báo");
                    txt_tencachdung_cd.Text = "";
                    Form_CachDung();
                }
                else
                {
                    MessageBox.Show("Số lượng cách dùng vượt quá qui định!", "Thông báo");
                    txt_tencachdung_cd.Text = "";
                }
            }
            catch
            {
                MessageBox.Show("Dữ liệu nhập bị lỗi!", "Thông báo");
            }
        }
        private void DGV_DanhSachCachDung_SelectionChanged(object sender, EventArgs e)
        {
            //txt_tencachdung_cd.Text = DGV_DanhSachCachDung.SelectedRows[0].Cells[1].Value.ToString();

        }
        private void button3_Click(object sender, EventArgs e)
        {
            string ma = DGV_DanhSachCachDung.SelectedRows[0].Cells[0].Value.ToString();
            string ten = txt_tencachdung_cd.Text;
            Boolean kq = new CachDungBUS().UpDateCachDung(ma, ten);
            Form_CachDung();
        }
        private void DGV_DanhSachCachDung_MouseClick(object sender, MouseEventArgs e)
        {
            txt_tencachdung_cd.Text = DGV_DanhSachCachDung.SelectedRows[0].Cells[1].Value.ToString();
        }
        #endregion

        private void DGV_DanhSachCachDung_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        #region Thông tin thuốc
        // Them thuoc moi
        public void Form_Thuoc()
        {
            List<Thuoc> l = new List<Thuoc>();
            l = new ThuocBUS().GetAll();
            DGV_DanhSachThuoc.DataSource = l;

        }

        private void btn_them_thuoc_Click(object sender, EventArgs e)
        {
            string ma = new ThuocBUS().MaBenhThuocMoi();
            string ten = txt_Tenthuoc_thuoc.Text;
            if (new ThuocBUS().InsertThuoc(ma, ten) == true)
            {
                MessageBox.Show("Đã thêm thuốc mới thành công");
                txt_Tenthuoc_thuoc.Text = "";
                Form_Thuoc();
        

            }
            else
                MessageBox.Show("Số lượng thuốc vượt quá qui định");
        }
        private void DGV_DanhSachThuoc_MouseClick(object sender, MouseEventArgs e)
        {
            txt_Tenthuoc_thuoc.Text = DGV_DanhSachThuoc.SelectedRows[0].Cells["dgvtxt_tenthuoc"].Value.ToString();
        }
        private void btn_sua_thuoc_Click(object sender, EventArgs e)
        {
            string ma = DGV_DanhSachThuoc.SelectedRows[0].Cells["txt_STT"].Value.ToString();
            string ten = txt_Tenthuoc_thuoc.Text;
            ThuocBUS bus = new ThuocBUS();
            bus.UpDateThuoc(ma, ten);
            Form_Thuoc();
        }
        #endregion





        #region Thog tin don vi
        public void Form_DonVi()
        {
            List<DonVi> l = new DonViBUS().GetAll();
            DGV_DanhSachDonVi.DataSource = l;
        }
        private void btn_Them_dv_Click(object sender, EventArgs e)
        {
            string ten = txt_tendonvi_dv.Text;
            string ma = new DonViBUS().GetMaDonVi();
            if (new DonViBUS().InsertDonVi(ma, ten) == true)
            {
                MessageBox.Show("Đã thêm đơn vị mới thành công");
                txt_tendonvi_dv.Text = "";
                Form_DonVi();
            }
            else
            {
                MessageBox.Show("Số lượng đơn vị vượt quá qui định!");
                txt_tendonvi_dv.Text = "";
            }
        }
        private void DGV_DanhSachDonVi_MouseClick(object sender, MouseEventArgs e)
        {
            txt_tendonvi_dv.Text = DGV_DanhSachDonVi.SelectedRows[0].Cells[1].Value.ToString();

        }
        private void btn_sua_dv_Click(object sender, EventArgs e)
        {
            string ma = DGV_DanhSachDonVi.SelectedRows[0].Cells[0].Value.ToString();
            string ten = txt_tendonvi_dv.Text;
            DonViBUS bus = new DonViBUS();
            bus.SuaDV(ma, ten);
            Form_DonVi();

        }
        #endregion



        #region thong tin cho tab don gia thuoc
        // lay danh sach thuoc
        public void GetAllThuoc()
        {
            List<Thuoc> list = new List<Thuoc>();
            list = new ThuocBUS().GetAll();
            cb_tenthuoc_dg.DataSource = list;
            cb_tenthuoc_dg.DisplayMember = "Tenthuoc";
            cb_tenthuoc_dg.ValueMember = "Mathuoc";
        }
        public void GetAllDonVi()
        {
            List<DonVi> list = new List<DonVi>();
            list = new DonViBUS().GetAll();
            cbb_donvi_dg.DataSource = list;
            cbb_donvi_dg.DisplayMember = "Tendonvi";
            cbb_donvi_dg.ValueMember = "Madonvi";
        }
        private void cbb_mathuoc_dg_SelectedIndexChanged(object sender, EventArgs e)
        {
            //txt_tenthuoc_dg.Text = cbb_mathuoc_dg.SelectedValue.ToString();

        }
        public void Form_DonGiaThuoc()
        {
            GetAllThuoc();
            GetAllDonVi();
            //txt_tenthuoc_dg.Text = cbb_mathuoc_dg.SelectedValue.ToString();
            List<DonGiaThuoc> list = new DonGiaThuocBUS().getAll();
            DGV_DanhSachDonGiaThuoc.DataSource = list;
            DGV_DanhSachDonGiaThuoc.Columns["Mathuoc"].Visible = false;
            DGV_DanhSachDonGiaThuoc.Columns["Madonvi"].Visible = false;
        }
        private void btn_them_dg_Click_1(object sender, EventArgs e)
        {
            try
            {
                string mathuoc = cb_tenthuoc_dg.SelectedValue.ToString();
                string madonvi = cbb_donvi_dg.SelectedValue.ToString();
                int soluong = int.Parse(txt_soluong_dg.Text);
                double dongia = double.Parse(txt_dongia_dg.Text);
                if (new DonGiaThuocBUS().InsertDonGiaThuoc(mathuoc, madonvi, soluong, dongia) == true)
                {
                    MessageBox.Show("Thêm thành công đơn giá cho thuốc");
                    txt_dongia_dg.Text = "00000";
                    txt_soluong_dg.Text = "0";
                    Form_DonGiaThuoc();
                }
                else
                {
                    MessageBox.Show("Dữ liệu nhập chưa đúng!");
                }

            }
            catch
            {
                MessageBox.Show("Dữ liệu nhập chưa đúng!");
            }
        }
        private void DGV_DanhSachDonGiaThuoc_MouseClick(object sender, MouseEventArgs e)
        {
            cb_tenthuoc_dg.SelectedValue = DGV_DanhSachDonGiaThuoc.SelectedRows[0].Cells[1].Value;
            cbb_donvi_dg.SelectedValue = DGV_DanhSachDonGiaThuoc.SelectedRows[0].Cells[2].Value;
            txt_dongia_dg.Text = DGV_DanhSachDonGiaThuoc.SelectedRows[0].Cells[3].Value.ToString();
            txt_soluong_dg.Text = DGV_DanhSachDonGiaThuoc.SelectedRows[0].Cells[4].Value.ToString();

        }
        private void btn_sua_dg_Click(object sender, EventArgs e)
        {
            string ma = DGV_DanhSachDonGiaThuoc.SelectedRows[0].Cells[0].Value.ToString();
            string mathuoc = cb_tenthuoc_dg.SelectedValue.ToString();
            string madonvi = cbb_donvi_dg.SelectedValue.ToString();
            int soluong = int.Parse(txt_soluong_dg.Text);
            double dongia = double.Parse(txt_dongia_dg.Text);
            DonGiaThuoc dto = new DonGiaThuoc(ma, mathuoc, madonvi, soluong, dongia);
            new DonGiaThuocBUS().capnhatDG(dto);
            Form_DonGiaThuoc();

        }
        #endregion

        private void DGV_DanhSachDonGiaThuoc_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void groupBox18_Enter(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
        #region  thong tin tab phieu kham benh
        //lay thong tin danh sach tat ca ma kham benh co cung ngay kham
        private string maPK;
        public void LoadDanhSachKhamBenhTheoNgayKham(DateTime ngay)
        {
            ccb_makhambenh_pk.ResetText();
            txt_hoten_pk.Text = "";
            List<KhamBenh> list = new List<KhamBenh>();
            list = new KhamBenhBUS().GetKhamBenhTheoNgayKham(ngay);
            ccb_makhambenh_pk.DataSource = list;
            ccb_makhambenh_pk.DisplayMember = "MaKhamBenh";
            ccb_makhambenh_pk.ValueMember = "MaKhamBenh";


        }
        // Lay danh sach cac loai benh cho phieu kham benh
        public void DanhSachLoaiBenhChoPhieuKhamBenh()
        {
            List<LoaiBenh> list = new List<LoaiBenh>();
            list = new LoaiBenhBUS().GetAll();
            cbb_loaibenh_pk.DataSource = list;
            cbb_loaibenh_pk.DisplayMember = "Tenloaibenh";
            cbb_loaibenh_pk.ValueMember = "Maloaibenh";
        }

        //load tab phieu kham benh
        public void Form_PhieuKhamBenh()
        {
            dti_ngaykham_pk.Text = DateTime.Now.Date.ToString();
            LoadDanhSachKhamBenhTheoNgayKham(DateTime.Now.Date);
            DanhSachLoaiBenhChoPhieuKhamBenh();

            maPK = new PhieuKhamBUS().GetMaPhieuKham();
        }
        private void dti_ngaykham_pk_ValueChanged(object sender, EventArgs e)
        {
            LoadDanhSachKhamBenhTheoNgayKham(dti_ngaykham_pk.Value);
            if (txt_hoten_pk.Text == "")
            {
                btn_DonThuoc_pk.Enabled = false;

            }
            else
            {
                btn_DonThuoc_pk.Enabled = true;

            }
            if (txt_Trieuchung_pk.Text == "" || txt_hoten_pk.Text == "")
            {
                btn_lapphieu_pk.Enabled = false;

            }
            else
                btn_lapphieu_pk.Enabled = true;
        }
        private void ccb_makhambenh_pk_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            txt_hoten_pk.Text = new BenhNhanBUS().GetHoTenBenhNhanTheoMaKhamBenh(ccb_makhambenh_pk.SelectedValue.ToString());

            // xử lý ẩn hiện nút
            if (txt_hoten_pk.Text == "")
            {
                btn_DonThuoc_pk.Enabled = false;
            }
            else
                btn_DonThuoc_pk.Enabled = true;
        }
        // Bien toan cuc cho tab phieu kham benh
        private BindingList<ChiTietPhieuKham> listchitietphieukham = new BindingList<ChiTietPhieuKham>();
        private void btn_DonThuoc_pk_Click_1(object sender, EventArgs e)
        {
            DONTHUOCMOI fr = new DONTHUOCMOI();
            fr.Maphieukham = new PhieuKhamBUS().GetMaPhieuKham();
            fr.Maloaibenh = cbb_loaibenh_pk.SelectedValue.ToString();
            fr.ShowDialog();
            BindingList<ChiTietPhieuKham> list = new BindingList<ChiTietPhieuKham>();
            list = fr.Listchitietphieukham;
            listchitietphieukham = list;
        }
        // Xử lý nút lập phiếu
        private void txt_Trieuchung_pk_TextChanged_1(object sender, EventArgs e)
        {
            if (txt_Trieuchung_pk.Text == "" || txt_hoten_pk.Text == "")
            {
                btn_lapphieu_pk.Enabled = false;

            }
            else
                btn_lapphieu_pk.Enabled = true;
        }
        private void btn_lapphieu_pk_Click_1(object sender, EventArgs e)
        {
            try
            {
                string ma = maPK;
                string makhambenh = ccb_makhambenh_pk.Text;
                string trieuchung = txt_Trieuchung_pk.Text;
                if (new PhieuKhamBUS().InsertPhieuKham(ma, makhambenh, trieuchung) == true)
                {
                    new ChiTietPhieuKhamBUS().InsertDanhSachPhieuKHam(listchitietphieukham);
                    MessageBox.Show("Thiết lập thành công");
                    txt_Trieuchung_pk.Text = "";
                    maPK = new PhieuKhamBUS().GetMaPhieuKham();

                }

            }
            catch
            {
                MessageBox.Show("Chưa thiết lập được phiếu khám");
            }
        }
        #endregion

        private void đăngNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DangNhap fr = new DangNhap();
            fr.ShowDialog();
            if (Program.flag == true)
            {
                tab_Quanlydulieu.Enabled = true;
                int phanquyen = Convert.ToInt16(fr.Maphanquyen);
                Form_KhamBenh();
                Form_BenhNhan();
                Form_TiepNhanLoaiBenh();
                Form_CachDung();
                Form_Thuoc();
                Form_DonVi();
                Form_DonGiaThuoc();
                Form_PhieuKhamBenh();
                Form_HoaDon();
                Form_BaoCaoDoanhThu();
                Form_ThayDoiQuiDinh();
                Form_BaoCaoSuDungThuoc();
                if (phanquyen == 2)
                {
                    btn_phanquyen.Enabled = true;
                    tab_baocaodoanhthu.Enabled = true;
                    tab_baocaosudungthuoc.Enabled = true;
                    tab_quidinh.Enabled = true;
                    tab_dongiathuoc.Enabled = true;
                }
                if (phanquyen == 0)
                {
                    tab_BenhNhan.Enabled = true;
                    tab_KhamBenh.Enabled = true;
                }
                if (phanquyen == 1)
                {
                    tab_PhieuKham.Enabled = true;
                    tab_cachdung.Enabled = true;
                    tab_loaibenh.Enabled = true;
                    tab_donvi.Enabled = true;
                    tab_hoadon.Enabled = true;
                }
            }
        }

        private void btn_phanquyen_Click(object sender, EventArgs e)
        {
            Fr_PhanHeNguoiDung f = new Fr_PhanHeNguoiDung();
            f.ShowDialog();
        }

        




        #region thong tin tab hoa don
        //lay thong tin ho ten benh nhan theo ma kham benh
        public void LoadMaKhamBenhThanhToanHoaDon(DateTime ngay)
        {
            cbb_Makhambenh_hd.ResetText();
            txt_hoten_hd.Text = "";
            List<PhieuKham> list = new List<PhieuKham>();
            list = new PhieuKhamBUS().DanhSachPhieuKhamCungNGay(ngay);
            cbb_Makhambenh_hd.DataSource = list;
            cbb_Makhambenh_hd.DisplayMember = "MaPhieuKham";
            cbb_Makhambenh_hd.ValueMember = "Makhambenh";
            double tienthuoc = new ChiTietPhieuKhamBUS().GetTienThuoc(cbb_Makhambenh_hd.Text);
            if (txt_tienkham_hd.Text != "")
            {
                txt_tienthuoc_hd.Text = tienthuoc.ToString();
                double tongtien = tienthuoc + int.Parse(txt_tienkham_hd.Text);
                txt_thanhtien_hd.Text = tongtien.ToString();
            }
        }
        // Load du lieu len gridview
        public void DanhSachChiTietThuoc()
        {
            dgvcbb_mathuoc_hd.DataSource = new ThuocBUS().GetAll();
            dgvcbb_mathuoc_hd.DisplayMember = "Tenthuoc";
            dgvcbb_mathuoc_hd.DataPropertyName = "MaLoaiThuoc";
            dgvcbb_mathuoc_hd.ValueMember = "Mathuoc";
            List<ChiTietPhieuKham> list = new ChiTietPhieuKhamBUS().GetDanhSachChiTietPhieuKham(cbb_Makhambenh_hd.Text);
            dgv_danhsachchitietthuoc.DataSource = list;
            dgv_danhsachchitietthuoc.Columns["Machitiet"].Visible = false;
            dgv_danhsachchitietthuoc.Columns["Maphieukham"].Visible = false;
            dgv_danhsachchitietthuoc.Columns["MaLoaiBenh"].Visible = false;
            dgv_danhsachchitietthuoc.Columns["Madonvi"].Visible = false;
            dgv_danhsachchitietthuoc.Columns["Tendonvi"].Visible = false;
            dgv_danhsachchitietthuoc.Columns["Tencachdung"].Visible = false;
            dgv_danhsachchitietthuoc.Columns["Tenthuoc"].Visible = false;
            dgv_danhsachchitietthuoc.Columns["Macachdung"].Visible = false;
            danhsothutuchodatagridview(dgv_danhsachchitietthuoc, "STT_hd");

        }
        //lay thong tin ho ten benh nhan thanh toan hoa don
        public void GetHotenBenhNhanThanhToanHoaDon(string makhambenh)
        {
            txt_hoten_hd.Text = new BenhNhanBUS().GetHoTenBenhNhanTheoMaKhamBenh(makhambenh);

        }
        public void Form_HoaDon()
        {
            LoadMaKhamBenhThanhToanHoaDon(DateTime.Now.Date);
            dti_ngaykham_hd.Text = DateTime.Now.Date.ToString();
            ThongTinTienKham();
            DanhSachChiTietThuoc();
        }

        private void cbb_Makhambenh_hd_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            txt_hoten_hd.Text = new BenhNhanBUS().GetHoTenBenhNhanTheoMaKhamBenh(cbb_Makhambenh_hd.SelectedValue.ToString());
        }

        private void dti_ngaykham_hd_ValueChanged(object sender, EventArgs e)
        {
            LoadMaKhamBenhThanhToanHoaDon(dti_ngaykham_hd.Value);
            DanhSachChiTietThuoc();
        }
        // Tiền khám 
        public void ThongTinTienKham()
        {
            double tienkham = new ThamSoBUS().GetTienKham();
            txt_tienkham_hd.Text = tienkham.ToString();
        }
        private void btn_thanhtoan_dh_Click_1(object sender, EventArgs e)
        {
            string maphieukham = cbb_Makhambenh_hd.Text;
            double tienthuoc = double.Parse(txt_tienthuoc_hd.Text);
            double thanhtien = double.Parse(txt_thanhtien_hd.Text);
            if (new HoaDonBUS().InsertHoaDon(maphieukham, tienthuoc, thanhtien) == true)
            {

                txt_thanhtien_hd.Text = "";
                txt_tienthuoc_hd.Text = "";
                dgv_danhsachchitietthuoc.AllowUserToDeleteRows = true;
                List<ChiTietPhieuKham> list = new List<ChiTietPhieuKham>();
                dgv_danhsachchitietthuoc.DataSource = list;
                MessageBox.Show("Thanh toán hoàn tất", "Thông báo");
            }
            else
            {
                MessageBox.Show("Thanh toán bị lỗi !", "Thông báo");
            }
        }
        #endregion
        #region thong tin Thay Doi Qui Dinh Phong kham
        public void Form_ThayDoiQuiDinh()
        {
            txt_tienkham_qd.Text = new ThamSoBUS().GetTienKham().ToString();
            txt_slbenhnhantoida_qd.Text = new ThamSoBUS().GetBenhNhanToiDa().ToString();

        }
        private void btn_CapNhat_qd_Click(object sender, EventArgs e)
        {
            try
            {
                int slbenhnhan = int.Parse(txt_slbenhnhantoida_qd.Text);
                double tienkham = double.Parse(txt_tienkham_qd.Text);

                if (new ThamSoBUS().InsertQuiDInh(slbenhnhan, tienkham) == true)
                {
                    MessageBox.Show("Cập nhật thành công!", "Thông báo");
                    Form_ThayDoiQuiDinh();
                }
            }
            catch
            {
                MessageBox.Show("Dữ liệu nhập chưa đúng!", "Thông báo");
            }

        }
        #endregion

        private void btn_xemdonthuoc_pk_Click(object sender, EventArgs e)
        {

        }
        #region Bao cáo doanh thu
        // Thong tin bao cao daonh thu theo ngay
        public void Form_BaoCaoDoanhThu()
        {
            List<int> list = new List<int>();
            for (int i = 1; i < 13; i++)
            {
                list.Add(i);
            }
            cbb_thang_bcdt.DataSource = list;
        }
        private void btn_hienthi_baodoanhthu_Click_1(object sender, EventArgs e)
        {
            List<ChiTietBaoCaoDoanhThu> list = new List<ChiTietBaoCaoDoanhThu>();
            try
            {
                int thang = Convert.ToInt32(cbb_thang_bcdt.Text);
                list = new ChiTietBaoCaoDoanhThuBUS().DoanhThuBaoCaoTheoNgay(thang);
                RPT_DanhThuTheoNgay rpt = new RPT_DanhThuTheoNgay();
                rpt.SetDataSource(list);
                crv_baocaodoanhthu.ReportSource = rpt;
            }
            catch
            {
                RPT_DanhThuTheoNgay rpt = new RPT_DanhThuTheoNgay();
                rpt.SetDataSource(list);
                crv_baocaodoanhthu.ReportSource = rpt;
            }
        }
        #endregion
        #region Bao Cáo Su Dung Thuoc
        public void Form_BaoCaoSuDungThuoc()
        {
            List<int> list = new List<int>();
            for (int i = 1; i < 13; i++)
            {
                list.Add(i);
            }
            cbb_thang_bct.DataSource = list;
        }
        private void button9_Click(object sender, EventArgs e)
        {
            List<BaoCaoSuDungThuoc> list = new List<BaoCaoSuDungThuoc>();
            try
            {
                int thang = Convert.ToInt32(cbb_thang_bct.Text);

                list = new BaoCaoSuDungThuocBUS().DanhSachBaoCaoSuDungThuoc(thang);
                RPT_BaoCaoSuDungThuoc rpt = new RPT_BaoCaoSuDungThuoc();
                rpt.SetDataSource(list);
                crv_baocaosudungthuoc.ReportSource = rpt;
            }
            catch
            {
                RPT_BaoCaoSuDungThuoc rpt = new RPT_BaoCaoSuDungThuoc();
                rpt.SetDataSource(list);
                crv_baocaosudungthuoc.ReportSource = rpt;
            }
        }
        #endregion

        
        
        

        
        

        

        

        

        

        
       

        
        

        

        
        

    
      

        

       

        
        

        

       

       


  
    }
}
